package org.xhy.sniffer.match;


import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;

import java.util.Arrays;
import java.util.List;

/**
 * @description: 类名匹配器
 * @gitee: https://gitee.com/XhyQAQ
 * @copyright: B站: https://space.bilibili.com/152686439
 * @Author: Xhy
 * @CreateTime: 2024-07-11 19:16
 */
public class ClassNameMatch implements ClassMatch{

    List<String> classNames;

    public ClassNameMatch(String... classNames){
        this.classNames = Arrays.asList(classNames);
    }

    // 类名可多匹配
    @Override
    public ElementMatcher<? super TypeDescription> buildJunction() {
        ElementMatcher.Junction<? super TypeDescription> junction = null;
        for (String className : classNames) {
            if (junction == null){
                junction = ElementMatchers.named(className);
            }else{
                junction.or(ElementMatchers.named(className));
            }
        }
        return junction;
    }

    @Override
    public boolean isMatch(TypeDescription typeDescription) {
        return classNames.contains(typeDescription.getTypeName());
    }

    public static ClassMatch buildClassNameMatch(String... className) {
        return new ClassNameMatch(className);
    }
}
